package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    //查看详情
    @GetMapping("/detail")
    public Object detail(String id){
        strategyStatisVORedisService.viewnumIncrease(id,1);
        //攻略阅读数
        return JsonResult.success(strategyService.get(id));
    }

    //全部分页
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }

    //主题
    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.listAll());
    }

    //攻略评论
    @RequiredLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo){
        //设置用户信息
        BeanUtils.copyProperties(userInfo,strategyComment);
        strategyComment.setUserId(userInfo.getId());
        strategyCommentService.save(strategyComment);

        strategyStatisVORedisService.replynumIncrease(strategyComment.getStrategyId(),1);

        return JsonResult.success();
    }

    //分页
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    //攻略评论点赞
    @RequiredLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }

    //统计攻略数据
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVO(sid));
    }

    //收藏攻略
    @RequiredLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        boolean flag = strategyStatisVORedisService.favor(sid,userInfo.getId());
        return JsonResult.success(flag);
    }

    //攻略置顶
    @RequiredLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        boolean flag = strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(flag);
    }



}
